package com.ctshk.task.worker.processors;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ctshk.common.dto.Result;
import com.ctshk.rpc.order.localfun.service.ILocalFunOrderService;
import com.github.kfcfans.powerjob.worker.core.processor.ProcessResult;
import com.github.kfcfans.powerjob.worker.core.processor.TaskContext;
import com.github.kfcfans.powerjob.worker.core.processor.sdk.BasicProcessor;
import com.github.kfcfans.powerjob.worker.log.OmsLogger;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Component;

/**
 * 当地玩乐 超时订单取消任务
 */
@Slf4j
@Component
public class LocalFunOrderCancelProcessor implements BasicProcessor {

    @DubboReference
    private ILocalFunOrderService iLocalFunOrderService;

    @Override
    public ProcessResult process(TaskContext context) throws Exception {
        OmsLogger omsLogger = context.getOmsLogger();
        JSONObject jsonObject = JSON.parseObject(context.getInstanceParams());
        Long orderId = jsonObject.getLong("id");
        Result result = iLocalFunOrderService.updateOrderPaymentStatus(orderId);
        if (!result.isSuccess()) {
            omsLogger.error("订单超时,取消订单操作失败");
        } else {
            omsLogger.info("订单超时,取消订单操作成功");
        }
        // 根据控制台参数判断是否成功
        boolean success = !"failed".equals(context.getJobParams());
        omsLogger.info("LocalFunOrderCancelProcessor finished process,success: .", success);

        return new ProcessResult(success, context + ": " + success);
    }

}
